package com.example.li.myapplication;


import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.Subscriber;

/**
 * Created by ly on 2016/9/29 14:52.
 */

public class DownloadUtils {
    private OkHttpClient client;

    public DownloadUtils() {
        client = new OkHttpClient();
    }


    public Observable<byte[]> downloadImg(final String path) {
        return Observable.create(new OnSubscribe<byte[]>() {
            @Override
            public void call(final Subscriber<? super byte[]> subscriber) {
                if (!subscriber.isUnsubscribed()) {
                    Request request = new Request.Builder().url(path).build();
                    client.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            subscriber.onError(e); //接口
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            if (response.isSuccessful()) {
                                byte[] data = response.body().bytes();  //getData
                                if (data != null) {
                                    subscriber.onNext(data);  //操作接口
                                }

                            }
                            subscriber.onCompleted();
                        }
                    });
                }

            }
        });
    }

    public Observable<String> downloadImg1(final String url, final Map<String, String> parmas) {
        return Observable.create(new OnSubscribe<String>() {
            @Override
            public void call(final Subscriber<? super String> subscriber) {
                if (subscriber.isUnsubscribed()) {



                    /*getData*/
                    FormBody.Builder builder = new FormBody.Builder();
                    for (Map.Entry<String, String> entry :
                            parmas.entrySet()) {
                        builder.add(entry.getKey(), entry.getValue());
                    }
                    RequestBody requestBody = builder.build();
                    Request request = new Request.Builder().url(url).post(requestBody).build();
                    client.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            if (response.isSuccessful()) {
                                String data = response.body().toString();
                                subscriber.onNext(data); //操作data
                            }
                            subscriber.onCompleted();
                        }
                    });
  /*getData*/
                    String data = "data";  // 获取数据
                    subscriber.onNext(data); //操作data
                }
            }
        });
    }

}
